package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.Slideshow;
import cn.kgc.ssm.mapper.SlidShowMapper;
import cn.kgc.ssm.service.SlidShowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/16
 * @description
 */
@Service
public class SlidShowServiceImpl implements SlidShowService {

    @Resource
    private SlidShowMapper slidShowMapper;

    @Override
    public ResponseResult getslidePictureList() {

        List<Slideshow> slideshows = slidShowMapper.selectPictureList();


        return ResponseResult.success().data(slideshows);
    }

    @Override
    public ResponseResult slidePage(Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<Slideshow> slideshows = slidShowMapper.selectList();
        PageInfo<Slideshow> slideshowPageInfo = new PageInfo<>(slideshows);

        long total = slideshowPageInfo.getTotal();

        return ResponseResult.success().data(slideshows).count(total);
    }

    @Override
    public ResponseResult editSlide(Integer id) {

        slidShowMapper.updateSlideTime(id);
        return ResponseResult.success().message("置顶成功");
    }

    @Override
    public ResponseResult deleteSlide(Integer id, String uuid) {

        // 根据id  删除数据库中轮播图信息
        slidShowMapper.deleteById(id);

        // 根据uuid 删除本次磁盘中图片的数据
        File file = new File("D:/upload/slides/" + uuid);
        file.delete();

        return ResponseResult.success().message("删除成功");
    }

    @Override
    public ResponseResult addUploadInfo(Slideshow slideshow) {

        slidShowMapper.addUploadInfo(slideshow);
        return ResponseResult.success().message("上传成功");
    }
}
